package com.djl.sqtg.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        return result;
    }

    /**
     * 获取过去7天内的日期数组
     * @return  日期数组
     */
    public static ArrayList<String> pastDay(String time){
        ArrayList<String> pastDaysList = new ArrayList<>();
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            for (int i = 6; i >= 0; i--) {
                pastDaysList.add(getPastDate(i,date));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
        return pastDaysList;
    }

    /**
     * 获取现在的时间 YYYY-MM-DD HH:mm:ss
     * @return
     */
    public static String getNowTime() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdfTime.format(new Date());
    }

    /**
     * 获取今日日期 YYYY-MM-DD
     * @return
     */
    public static String getToday() {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");
        return sdfTime.format(new Date());
    }
    /**
     * 获取今日日期 YYYY-MM-DD
     * @return Date类型
     */
    public static Date getTodayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
             date = sdf.parse(DateUtil.getToday());
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        System.out.println("------------"+date);
        return  date;
    }



    /**
     * 把日期范围分割出来存于数组
     * @return
     */
    public static List<String> cutDate(String dateRange) {
        String[] dateArry = dateRange.split(" ~ ");
        for (String date:dateArry) {
            System.out.println(date);
        }
        String start = dateArry[0];
        String end = dateArry[1];
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dBegin = sdf.parse(start);
            Date dEnd = sdf.parse(end);
            return findDates(dBegin, dEnd);
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    public static List<String> findDates(Date dBegin, Date dEnd) throws Exception {
        List<String> listDate = new ArrayList<>();
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        listDate.add(new SimpleDateFormat("yyyy-MM-dd").format(dBegin));
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (calEnd.after(calBegin)) {
            calBegin.add(Calendar.DAY_OF_YEAR, 1);
            if (calEnd.after(calBegin))
                listDate.add(new SimpleDateFormat("yyyy-MM-dd").format(calBegin.getTime()));
            else
                listDate.add(new SimpleDateFormat("yyyy-MM-dd").format(calEnd.getTime()));
        }
        return listDate;
    }
}
